<template>
  <section class="todoapp">
    <!-- 头部部分 -->
    <ToHeader @addList="addList" />

    <!-- 主体部分 -->
    <ToBody
      :list="renderList"
      @delList="delList"
      @changeStatus="changeStatus"
    />

    <!-- 底部部分 -->
    <ToFooter
      @clearList="clearList"
      :leftLength="leftLength"
      :list="list"
      :type="type"
      @changeType="changeType"
    />
  </section>
</template>

<script>
import ToHeader from '@/components/ToHeader.vue';
import ToBody from '@/components/ToBody.vue';
import ToFooter from '@/components/ToFooter.vue';

export default {
  components: {
    ToHeader,
    ToBody,
    ToFooter,
  },
  //提供数据
  data() {
    return {
      type: 'all',
      list: [
        { id: 1, name: '吃饭', isDone: true },
        { id: 2, name: '睡觉', isDone: false },
        { id: 3, name: '打豆豆', isDone: true },
      ],
    };
  },
  methods: {
    //删除事件
    delList(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    //添加删除线事件
    changeStatus(id) {
      // console.log(id);
      const res = this.list.find((item) => item.id === id);
      res.isDone = !res.isDone;
    },
    //增加事件
    addList(value) {
      // console.log(value);
      const obj = { id: +new Date(), name: value, isDone: false };
      this.list.unshift(obj);
    },
    //清除已完成事件
    clearList() {
      this.list = this.list.filter((item) => item.isDone === false);
    },
    //改变状态
    changeType(type) {
      // console.log(type);
      this.type = type;
    },
  },
  //计算集合
  computed: {
    //剩余未完成的事件长度
    leftLength() {
      return this.list.filter((item) => item.isDone === false).length;
    },
    //根据不同的type类型展示
    renderList() {
      switch (this.type) {
        case 'all':
          return this.list;
        case 'active':
          return this.list.filter((item) => item.isDone === false);
        case 'completed':
          return this.list.filter((item) => item.isDone === true);
        default:
          return this.list;
      }
    },
  },
};
</script>

<style></style>
